//引入组件库
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");

var circleUrl = require("url/circle");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var open = require("helper/open");

var listview = ui("do_ListView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);


page.on("back", function() {
	app.closePage();
})

/** **************处理页面-头部-begin**************** */
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// 左上角-返回
ui(addheader + ".headerLeft").on("touch", function() {
	app.closePage()
});
// 头部标题
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "管理圈子的话题"
});
header.refreshData();

/** **************处理页面-头部-end**************** */

var circleId = null;
page.on("loaded", function(index) {
	circleId = page.getData().circleId;
	binddata();
	initTopicTagList();
})

//当前页码
var pageNum = 1;
//本页面-ajax业务请求url
var httpUrl = circleUrl.url.topic.list4manageCircle;

//全部刷新数据
function binddata(){
	pageNum=1;
	http.post(httpUrl,{circleId:circleId, page:pageNum},function(data){
		if(data.topicPage){
			listdata.removeAll();//清除数据
			listdata.addData(data.topicPage.content);
			listview.refreshItems();//刷新item数据
		}
	});
}

function getNextPageData(){
	pageNum++;
	http.post(httpUrl,{circleId:circleId, page:pageNum},function(data){
		if(data.topicPage){
			listdata.addData(data.topicPage.content);
			listview.refreshItems();
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		binddata();
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});

//点击一条话题
listview.on("touch", function(data){
	var onTopic = listdata.getOne(data);
	var topicId = onTopic.id;
	open.open("source://view/circle/topic/detail/topicBorderView.ui",{topicId:topicId});
});

page.on("doTop4Topic", function(data) {
	binddata();
});

//选择标签
var addPicker = ui("do_ALayout_root").add("picker","source://view/reuse/form/oneCategory.ui", 0, 0);
var picker = ui("picker");

/********选择标签begin**********/
var topicId4toTop = null;
page.on("to_select_topic_tag", function(data) {
	topicId4toTop = data;
	if(topicTagList && topicTagList.length>0){
		var ptitle = {"title" : "选择置顶标签","id" : "topicSelectTag"};
		var pcount = [ ptitle, topicTagList ];
		page.fire("oneCategoryList", pcount);
		page.fire("oneCategory", "true");
	}
});

page.on("refreshOneCategory", function(data) {
	if ("topicSelectTag" == data[1]) {//选择标签
		var tagName =  data[0].onename;
		page.fire("select_topic_tag_back", {topicId: topicId4toTop, tagName: tagName});
	}
});
/********选择标签end**********/

var topicTagList = [];
function initTopicTagList(){
	topicTagList = [];
	httpSilence.post(circleUrl.url.topic.tagList, {}, function(data) {
		var tagList = data.tagList;
		for(var i in tagList){
			topicTagList.push({"id":tagList[i].code,name:tagList[i].name});
		}
	});
}